package cn.wang.service.cargo.shippingorder.Impl;


import cn.wang.dao.BaseDao;
import cn.wang.entity.EchartPieVo;
import cn.wang.entity.cargo.packing.PackingList;
import cn.wang.entity.cargo.shippingorder.ShippingOrder;
import cn.wang.entity.sysadmin.User;
import cn.wang.service.cargo.shippingorder.ShippingOrderService;
import cn.wang.utils.Pagination;
import org.apache.struts2.ServletActionContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;


@Transactional
@Service
public class ShippingOrderServiceImpl implements ShippingOrderService {
    @Autowired
    private BaseDao<ShippingOrder,String> shippingOrderDao;
    @Autowired
    private BaseDao<PackingList,String>PackingListDao;

    @Override
    public Pagination findPage(Pagination page) {
    return shippingOrderDao.pageByHql("from ShippingOrder",page.getPageNo(),page.getPageSize(),null);
    }

    @Override
    public List<ShippingOrder> findAll() {
        return shippingOrderDao.getListByHQL("from ShippingOrder");
    }

    @Override
    public void save(ShippingOrder model) {
        //保存创建人和创建人部门
        User user = (User) ServletActionContext.getRequest().getSession().getAttribute("_CURRENT_USER");
        model.setCreateBy(user.getId());
        model.setCreateDept(user.getDept().getId());
        model.setCreateTime(new Timestamp(System.currentTimeMillis()));
        model.setState(0l);
        //设置双向关系
       PackingList packingList= PackingListDao.get(PackingList.class,model.getId());
        //设置值装箱单state为2
        packingList.setState(2l);
        PackingListDao.update(packingList);
        model.setPackingList(packingList);


        shippingOrderDao.save(model);
    }

    @Override
    public void deleteById(String id) {
        //设置装箱单状态为1
    shippingOrderDao.deleteById(ShippingOrder.class,id);

    }

    @Override
    public void submit(ShippingOrder model) {
       ShippingOrder shippingOrder =shippingOrderDao.get(ShippingOrder.class,model.getId());
        shippingOrder.setState(1l);
        shippingOrderDao.evcit(model);
        shippingOrderDao.update(shippingOrder);
    }

    @Override
    public void cancel(ShippingOrder model) {
        ShippingOrder shippingOrder =shippingOrderDao.get(ShippingOrder.class,model.getId());
        shippingOrder.setState(0l);
        shippingOrderDao.evcit(model);
        shippingOrderDao.update(shippingOrder);
    }

    @Override
    public void update(ShippingOrder model) {
        //查处老数据
        ShippingOrder shippingOrder= shippingOrderDao.get(ShippingOrder.class,model.getId());

        model.setCreateDept(shippingOrder.getCreateDept());
        model.setCreateTime(shippingOrder.getCreateTime());
        model.setCreateBy(shippingOrder.getCreateBy());
        //清楚缓存
        shippingOrderDao.evcit(shippingOrder);
    shippingOrderDao.update(model);
    }

    @Override
    public ShippingOrder findById(String id) {
    return shippingOrderDao.get(ShippingOrder.class,id);
    }
}
